package common;

import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;

import model.ShareFileModel;

/**
 * for client command 服务处理者.
 * 服务本身.
 *
 * @author guanweichen
 * @version 2010-12-21
 */
public abstract class Server extends Thread {
	
	protected Server server = null;
	protected int port;
	protected ServerSocket serverSocket;
	public static ResourceBundle rb = ResourceBundle.getBundle("init-conf");
	/** 当前命令 */
	protected Command cmd;
	/** 服务启动状态 */
	protected boolean started = false;
	/** 服务开放文件列表 */
	protected List<ShareFileModel> listShareFile = Collections.synchronizedList( new ArrayList<ShareFileModel>() );
	/** 服务连接人 */
	protected List<ClientModel> listClient = Collections.synchronizedList( new ArrayList<ClientModel>() );
	/** 客户端正在浏览的,文件更新通知队列 */
	protected List<FileUpNotifyQueue> listNotifyQueue = Collections.synchronizedList( new ArrayList<FileUpNotifyQueue>() );
	
	public abstract void startup(final Integer port);
	public abstract void shutdown();
	/**
	 * 注册当前连接的client.
	 * 
	 * @param client
	 * @return
	 */
	public abstract boolean addClient(ClientModel client);
	/**
	 * 注册共享文件.
	 * 
	 * @param shareFile
	 * @return
	 */
	public abstract boolean addShareFile(ShareFileModel shareFile);
	/**
	 * 注册clients 正在阅读的文件.
	 * 
	 * @param notifyQueue
	 * @return
	 */
	public abstract boolean addFileUpNotify(FileUpNotifyQueue notifyQueue);
	/**
	 * 删除已经没有启动的client.
	 * 
	 * @param client
	 * @return
	 */
	public abstract boolean removeClient(ClientModel client);
	/**
	 * 当文件不需要共享的时候.
	 * 删除服务器共享文件.
	 * 
	 * @param shareFile
	 * @return
	 */
	public abstract boolean removeShareFile(ShareFileModel shareFile);
	/**
	 * 删除文件更新通知队列.
	 * 该队列用于保存client正在查看的文件.
	 * 
	 * @param notifyQueue
	 * @return
	 */
	public abstract boolean removeFileUpNotify(FileUpNotifyQueue notifyQueue);
	public abstract boolean removeFileUpNotifyByAddressAndPort(String address, Integer port);
	
	public Server getServer() {
		return server;
	}
	public int getPort() {
		return port;
	}
	public ServerSocket getServerSocket() {
		return serverSocket;
	}
	public static ResourceBundle getRb() {
		return rb;
	}
	public Command getCmd() {
		return cmd;
	}
	public boolean isStarted() {
		return started;
	}
	public List<ShareFileModel> getListShareFile() {
		return listShareFile;
	}
	public List<ClientModel> getListClient() {
		return listClient;
	}
	public List<FileUpNotifyQueue> getListNotifyQueue() {
		return listNotifyQueue;
	}
	public void setServer(Server server) {
		this.server = server;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public void setServerSocket(ServerSocket serverSocket) {
		this.serverSocket = serverSocket;
	}
	public static void setRb(ResourceBundle rb) {
		Server.rb = rb;
	}
	public void setCmd(Command cmd) {
		this.cmd = cmd;
	}
	public void setStarted(boolean started) {
		this.started = started;
	}
	public void setListShareFile(List<ShareFileModel> listShareFile) {
		this.listShareFile = listShareFile;
	}
	public void setListClient(List<ClientModel> listClient) {
		this.listClient = listClient;
	}
	public void setListNotifyQueue(List<FileUpNotifyQueue> listNotifyQueue) {
		this.listNotifyQueue = listNotifyQueue;
	}
	
}
